package at.dl.android.dota.adviser;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.Toast;

public class SimpleImageAdapter extends BaseAdapter implements OnClickListener {
    protected Context context;
    protected int[] ids;
    protected String[] names;
    protected Map<ImageView, String> imageVie2TextMap = new HashMap<ImageView, String>();
    int count;
    
    SimpleImageAdapter ( Context c, int[] ids, String[] names ) {
        this.context = c;
        this.ids = ids;
        this.names = names;
        for ( count = 0; 
                 count < ids.length   && ids[count] != 0  
              && count < names.length && names[count] != null; 
              ++count );
    }

    @Override
    public int getCount () {
        return count;
    }

    @Override
    public Object getItem ( int arg0 ) {
        return null;
    }

    @Override
    public long getItemId ( int position ) {
        return 0;
    }

    @Override
    public View getView ( int position, View convertView, ViewGroup parent ) {
        ImageView imageView;
        if ( convertView == null ) {
            imageView = new ImageView(context);
            imageView.setPadding(8,8,8,8);
            imageView.setOnClickListener(this);
        }
        else {
            imageView = (ImageView) convertView;
        }
        imageView.setImageResource(ids[position]);
        imageVie2TextMap.put(imageView,names[position]);
        return imageView;
    }

    @Override
    public void onClick ( View v ) {
        CharSequence text = imageVie2TextMap.get(v);
        if ( text == null )
            return;
        handleSelectedName(text);        
    }

    protected void handleSelectedName ( CharSequence text ) {
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
    }
}
